package ca.uwindsor.cs.deepweb.estimation.method.border;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class CollectTrailResults {
	protected String inputDir;
	protected String outputFile;
	public CollectTrailResults(String input, String output) {
		inputDir =  input;
		outputFile = output;
	}
	
	public void start() throws Exception{
		File[] files = new File(inputDir).listFiles();
		InputStream inputStream = null;
		POIFSFileSystem fileSystem = null;
//		int[][] reuslt = new int[100][];
		
		HSSFWorkbook wb = new HSSFWorkbook();
		
		HSSFSheet sheetDA = wb.createSheet("esitmation");
		int i=0;
		for(File f: files){
			if(!f.getName().endsWith("xls")){
				continue;
			}
			inputStream = new FileInputStream(f);
			fileSystem = new POIFSFileSystem(inputStream);
//			
			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
			HSSFSheet sheet = workBook.getSheetAt(3);
			
			int DA=(int)sheet.getRow(0).getCell(2).getNumericCellValue();
			int DB=(int)sheet.getRow(1).getCell(2).getNumericCellValue();
			int DAB=(int)sheet.getRow(2).getCell(2).getNumericCellValue();
			int DownloadedDA = (int)sheet.getRow(3).getCell(1).getNumericCellValue();
			int DownloadedDB = (int)sheet.getRow(3).getCell(2).getNumericCellValue();
			int DownloadedDAB = (int)sheet.getRow(3).getCell(3).getNumericCellValue();
			int DownloadedD = (int)sheet.getRow(3).getCell(4).getNumericCellValue();
			int estimatedSize =(int)sheet.getRow(4).getCell(1).getNumericCellValue();

			HSSFRow row = sheetDA.createRow(i);
			HSSFCell cellTrail = row.createCell(0);
			cellTrail.setCellValue("Trail"+i);
			HSSFCell cellDA = row.createCell(1);
			cellDA.setCellValue(DA);
			HSSFCell cellDownloadedDA = row.createCell(2);
			cellDownloadedDA.setCellValue(DownloadedDA);
			
			HSSFCell cellDB = row.createCell(3);
			cellDB.setCellValue(DB);
			HSSFCell cellDownloadedDB = row.createCell(4);
			cellDownloadedDB.setCellValue(DownloadedDB);
			
			HSSFCell cellDAB = row.createCell(5);
			cellDAB.setCellValue(DAB);
			HSSFCell cellDownloadedDAB = row.createCell(6);
			cellDownloadedDAB.setCellValue(DownloadedDAB);
			
			HSSFCell cellDownloadedD = row.createCell(7);
			cellDownloadedD.setCellValue(DownloadedD);
			
			HSSFCell cellSize = row.createCell(8);
			cellSize.setCellValue(estimatedSize);
			i++;
		}
		OutputStream out = new FileOutputStream(outputFile);
		wb.write(out);
		out.close();
	}
	
	public static void main(String[] args){
		CollectTrailResults ctr = new CollectTrailResults(args[0],args[1]);
		try {
			ctr.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
